diff --git a/.github/workflows/cpp-ci-serial-programs-base.yml b/.github/workflows/cpp-ci-serial-programs-base.yml index efb3c80a04..f8a299851d 100644 --- a/.github/workflows/cpp-ci-serial-programs-base.yml +++ b/.github/workflows/cpp-ci-serial-programs-base.yml @@ -245,7 +245,9 @@ jobs: uses: actions/checkout@v7 if: inputs.run-tests with: - repository: 'PokemonAutomation/CommandLineTests' + # The fork's fixtures: upstream's plus the SystemKeyboardDetector screenshots. + repository: 'dayfine/CommandLineTests' + ref: '5de4ff774f9141d904c4b5c557fa7b9dc85eaef1' 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/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 62d006eb4c..b9258b6165 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -230,6 +230,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json) +pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/GateTestMaps + Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h PokemonAutomation::Pokemon navigation_gate_test_map_json) # Create binary target 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 75% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEventTypes.h rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEventTypes.h index 45988abc4e..73b4b12a77 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. @@ -92,9 +83,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 db2e5fbfa7..8269377758 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){ @@ -78,7 +81,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; @@ -86,7 +89,7 @@ std::string describe(const ScreenEvents& events){ continue; } if (!out.empty()){ - out += ", "; + out += separator; } out += screen_event_name(event); } @@ -148,23 +151,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) @@ -199,5 +185,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 92% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_ScreenEvents_Tests.cpp rename to SerialPrograms/Source/CommonTools/ScreenEvents/ScreenEvents_Tests.cpp index f7c7e32402..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{ @@ -146,7 +147,7 @@ struct ScreenEventsCase{ 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{ @@ -183,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 e56c4a593b..ed3655776b 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,11 @@ #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_SystemKeyboardDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" @@ -36,6 +39,7 @@ #include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" #include "Pokemon/Navigation/Pokemon_ConnectedMaps_Tests.h" +#include "Pokemon/Navigation/Pokemon_Gates_Tests.h" #include "Pokemon/Navigation/Pokemon_LegPlanner_Tests.h" #include "Pokemon/Navigation/Pokemon_LegWalker_Tests.h" #include "Pokemon/Navigation/Pokemon_MapData_Tests.h" @@ -68,6 +72,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ 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); @@ -82,12 +87,14 @@ 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); Pokemon::add_tests_WallRecovery(ret); Pokemon::add_tests_MapData(ret); Pokemon::add_tests_ConnectedMaps(ret); + Pokemon::add_tests_Gates(ret); Pokemon::add_tests_MoveMechanics(ret); Pokemon::add_tests_RuntimeMap(ret); Pokemon::add_tests_RouteLog(ret); @@ -96,6 +103,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_AutoStory(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); + NintendoSwitch::add_tests_SystemKeyboardDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); NintendoSwitch::PokemonBDSP::add_tests_DialogDetector(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_SystemKeyboardDetector.cpp b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp new file mode 100644 index 0000000000..b08bee33a4 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.cpp @@ -0,0 +1,280 @@ +/* System Keyboard Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#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/VideoOverlayScopes.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "Tests/ImageUnitTest.h" +#include "NintendoSwitch_SystemKeyboardDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +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 + + + +SystemKeyboardDetector::SystemKeyboardDetector(Color color) + : m_color(color) + , 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_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){ + // 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 (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.103, 0.240, 0.657}) || is_grey(ok, 600, 690); +} + + +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 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++){ + if (is_cursor_pixel(keys.pixel(x, y))){ + count++; + sum_x += (double)x; + sum_y += (double)y; + } + } + } + // 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 std::nullopt; + } + 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 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{ + +// 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 (!keyboard){ + return true; + } + 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."); + } + 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); + } + // 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); + } + 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); + } + } +} + +} // namespace + + +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); + } + add_cursor_tests(database); + add_planned_tests(database); +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h new file mode 100644 index 0000000000..3a5bbf7f03 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Inference/NintendoSwitch_SystemKeyboardDetector.h @@ -0,0 +1,99 @@ +/* System Keyboard Detector + * + * From: https://github.com/PokemonAutomation/ + * + * 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. + * + * 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 "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// 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); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + // 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; +}; + +// Finds SystemKeyboardDetector on the video, checking every 250 ms. +class SystemKeyboardWatcher : public DetectorToFinder{ +public: + SystemKeyboardWatcher(Color color = COLOR_CYAN) + : DetectorToFinder("SystemKeyboardWatcher", std::chrono::milliseconds(250), color) + {} +}; + + +// 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. +std::optional read_system_keyboard_cursor(const ImageViewRGB32& screen); + +// 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); + +// 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 +#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_StoryStepJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp index 3d39269e07..9b9f64665d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp @@ -323,8 +323,53 @@ DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source) return result; } +GateChange read_gate_change(const JsonValue& value, std::string_view source){ + const JsonObject& root = fields(value, {"opened", "switches"}, source); + GateChange result; + if (const JsonValue* field = root.get_value("opened")){ + for (std::string& key : read_strings(*field, source)){ + result.opened.insert(std::move(key)); + } + } + if (const JsonValue* field = root.get_value("switches")){ + for (const auto& [id, on] : field->to_object_throw(std::string(source))){ + result.switches[id] = on.to_boolean_throw(std::string(source)); + } + } + return result; +} + +JsonValue write_gate_change(const GateChange& change){ + JsonObject object; + if (!change.opened.empty()){ + JsonArray opened; + for (const std::string& key : change.opened){ + opened.push_back(key); + } + object["opened"] = std::move(opened); + } + if (!change.switches.empty()){ + JsonObject switches; + for (const auto& [id, on] : change.switches){ + switches[id] = on; + } + object["switches"] = std::move(switches); + } + return object; +} + +GateState read_gate_state(const JsonValue& value, std::string_view source){ + GateState result; + apply_gate_change(result, read_gate_change(value, source)); + return result; +} + +JsonValue write_gate_state(const GateState& state){ + return write_gate_change(gate_change_between(GateState(), state)); +} + StoryStep read_story_step(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after"}, source); + const JsonObject& root = fields(value, {"id", "kind", "why", "action", "args", "skip_if", "declared_after", "gates_after"}, source); StoryStep result; result.name = root.get_string_throw("id"); if (result.name.empty()){ @@ -344,11 +389,14 @@ StoryStep read_story_step(const JsonValue& value, std::string_view source, std:: if (const JsonValue* field = root.get_value("declared_after")){ result.pose_after = read_declared_pose(*field, source); } + if (const JsonValue* field = root.get_value("gates_after")){ + result.gates_after = read_gate_change(*field, source); + } return result; } StoryCheckpoint read_story_checkpoint(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "title", "start", "events", "metadata"}, source); + const JsonObject& root = fields(value, {"id", "title", "start", "start_gates", "events", "metadata"}, source); StoryCheckpoint result; result.id = root.get_string_throw("id"); if (result.id.empty()){ @@ -358,6 +406,9 @@ StoryCheckpoint read_story_checkpoint(const JsonValue& value, std::string_view s if (const JsonValue* start = root.get_value("start")){ result.start = read_declared_pose(*start, source); } + if (const JsonValue* gates = root.get_value("start_gates")){ + result.start_gates = read_gate_state(*gates, source); + } std::set names; result.steps_json = root.get_value_throw("events").dump(-1); for (const JsonValue& event : root.get_array_throw("events")){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h index a67f5df31c..5bc64ab097 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h @@ -36,6 +36,20 @@ GridPose read_grid_pose(const JsonValue& value, std::string_view source); DeclaredPose read_declared_pose(const JsonValue& value, std::string_view source); +// Read a gate change, {"opened": ["SilphCo_5F/door_1"], "switches": {"mansion": true}} +// (the gates a step opens, the switches it sets; both optional, unknown +// fields fail) and write it back. +GateChange read_gate_change(const JsonValue& value, std::string_view source); +// The change as JSON, as read_gate_change() reads it. +JsonValue write_gate_change(const GateChange& change); + +// The same as a state: what a checkpoint's "start_gates" says is open and +// on. "switches" that are off need not be listed, and are not written. +GateState read_gate_state(const JsonValue& value, std::string_view source); +// The state as JSON, as read_gate_state() reads it. +JsonValue write_gate_state(const GateState& state); + + // Read a step, its args parsed against its action among `actions` (by // default the built-in ones; a game passes them with its own) and their // fallbacks filled in. Unknown fields, kinds, actions and args, mistyped diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp index 69f213ad13..9eb48cba9b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson_Tests.cpp @@ -134,6 +134,38 @@ const WireCase CASES[] = { const DeclaredPose start = ZoneDeclared{.place = "HotelZ", .spot = std::nullopt, .heading = std::nullopt}; return expect(checkpoint.start == start && checkpoint.steps.size() == 1 && checkpoint.steps[0].kind == StoryStepKind::MASH, "a zone start and one scene"); }}}, + {.input = {.name = "step opens gates", .json = R"({"id":"unlock","kind":"talk","gates_after":{"opened":["SilphCo_5F/door_1"],"switches":{"mansion":true}}})"}, .expectation = {.check = [](const ReadRecord& record){ + const StoryStep& step = step_of(record); + GateChange expected; + expected.opened.insert("SilphCo_5F/door_1"); + expected.switches["mansion"] = true; + return expect(step.gates_after && *step.gates_after == expected, "door_1 opened and the mansion switch on"); + }}}, + {.input = {.name = "step with no gates", .json = R"({"id":"plain","kind":"talk"})"}, .expectation = {.check = [](const ReadRecord& record){ + return expect(!step_of(record).gates_after, "no gate change"); + }}}, + {.input = {.name = "gate change with an unknown field", .json = R"({"id":"x","kind":"talk","gates_after":{"closed":["a/b"]}})"}, .expectation = {.rejected = true}}, + {.input = {.name = "checkpoint gate state", .record = Record::CHECKPOINT, .json = R"({"id":"16.02","title":"Silph","start_gates":{"opened":["SilphCo_5F/door_1"]},"events":[{"id":"a","kind":"talk","gates_after":{"opened":["SilphCo_5F/door_2"]}},{"id":"b","kind":"talk","gates_after":{"switches":{"mansion":true}}},{"id":"c","kind":"talk"}]})"}, .expectation = {.check = [](const ReadRecord& record){ + const StoryCheckpoint& checkpoint = std::get(record); + if (!checkpoint.start_gates || checkpoint.start_gates->opened.size() != 1){ + return std::string("Expected start_gates with door_1 open."); + } + const GateState none = checkpoint_gates(checkpoint, 0); + const GateState first = checkpoint_gates(checkpoint, 1); + const GateState all = checkpoint_gates(checkpoint, 3); + if (!(none == *checkpoint.start_gates) || first.opened.size() != 2 || first.switch_on("mansion")){ + return std::string("Expected the start, then door_2 as well after one step."); + } + if (!all.switch_on("mansion") || !(checkpoint_gates(checkpoint, 99) == all)){ + return std::string("Expected the switch on after the steps, and past the end the same."); + } + const JsonValue written = write_gate_state(all); + return expect(read_gate_state(written, "round trip") == all, "the gate state to read back as written"); + }}}, + {.input = {.name = "checkpoint without gates", .record = Record::CHECKPOINT, .json = R"({"id":"x","title":"X","events":[{"id":"a","kind":"talk"}]})"}, .expectation = {.check = [](const ReadRecord& record){ + const StoryCheckpoint& checkpoint = std::get(record); + return expect(!checkpoint.start_gates && checkpoint_gates(checkpoint, 1) == GateState(), "no start gates and nothing open"); + }}}, {.input = {.name = "ordered checkpoint", .record = Record::CHECKPOINT, .json = R"({"id":"00.05","title":"Parcel","events":[ {"id":"first","kind":"talk"}, {"id":"second","kind":"scene"} ]})"}, .expectation = {.check = [](const ReadRecord& record){ const StoryCheckpoint& checkpoint = std::get(record); return expect(checkpoint.id == "00.05" && checkpoint.title == "Parcel" && !checkpoint.start && checkpoint.steps.size() == 2 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_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 7dfd736896..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; } @@ -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))}; } @@ -359,9 +359,9 @@ struct ScreenCase{ }; 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 e15bd54774..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; @@ -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; @@ -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 4e16d05e13..5aa882f997 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp @@ -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){ @@ -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()); @@ -301,13 +301,13 @@ struct WarpCase{ 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/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 0e51776983..b19c74a333 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -19,14 +19,15 @@ #include "Engine/Pokemon_StoryEngine_Tests.h" #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_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" @@ -42,8 +43,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); @@ -55,7 +58,6 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryEngine(database); add_tests_StoryEnginePolicy(database); add_tests_StoryEngineRuns(database); - add_tests_ScreenEvents(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..5f9df065b6 --- /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.display_text)); + 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..795655a996 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h @@ -0,0 +1,73 @@ +/* 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; + // The game's own text for the checkpoint, if its lines use one + // (checkpoint_start_line()). + std::string_view display_text = ""; +}; + +// 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..ac0a47ffd7 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointRetry.cpp @@ -0,0 +1,66 @@ +/* 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, std::nullopt, checkpoint_text)); + const CheckpointAttempts attempts{ + .max_attempts = policy.max_attempts, + .run = [&](size_t attempt){ + actions.log(checkpoint_attempt_line(checkpoint_text, attempt, policy.max_attempts, checkpoint_text)); + 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, std::nullopt, checkpoint_text)); + actions.reset(); + stats.m_reset++; + actions.update_stats(); + return true; + }, + .log = actions.log, + .display_text = checkpoint_text, + }; + 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_InterruptHandlers.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h index ffe9602115..8a0501aa1a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_InterruptHandlers.h @@ -17,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 57740f5820..69a2ccfbbe 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.cpp @@ -8,6 +8,7 @@ #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 cb46ce18d4..989460cf65 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts.h @@ -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 111d42035f..423d4769eb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_Interrupts_Tests.cpp @@ -16,7 +16,7 @@ #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{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.cpp index bc587ba860..6602895930 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{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h index 785dfe6950..4a10341023 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_SceneJudges.h @@ -2,7 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * - * Judgements a story step makes from screen events (Pokemon_ScreenEvents.h) + * 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 @@ -16,7 +16,7 @@ #include #include #include "Common/Cpp/Time.h" -#include "Pokemon_ScreenEvents.h" +#include "CommonTools/ScreenEvents/ScreenEvents.h" namespace PokemonAutomation{ namespace Pokemon{ 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_StoryInterrupts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp index 8c50b18d37..78ab78df44 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.cpp @@ -11,6 +11,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedException.h" +#include "CommonTools/ScreenEvents/ScreenFades.h" #include "Pokemon_StoryInterrupts.h" namespace PokemonAutomation{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h index 61945b8459..82f9d3b965 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryInterrupts.h @@ -37,7 +37,7 @@ #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" diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp index f931e3bd51..1e7059baee 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -20,44 +20,62 @@ 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 display text. +std::string checkpoint_prefix(std::string_view checkpoint, std::string_view display_text){ + if (!display_text.empty()){ + return std::string(display_text); + } + 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, std::string_view display_text +){ + const std::string prefix = checkpoint_prefix(checkpoint, display_text); 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, std::string_view display_text +){ + const std::string prefix = checkpoint_prefix(checkpoint, display_text); 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, std::string_view display_text){ 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, display_text), 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, std::string_view display_text +){ + const std::string prefix = checkpoint_prefix(checkpoint, display_text); + 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..4d68d72915 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,49 @@ 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 [Checkpoint] lines below name a checkpoint by its id. A game whose +// checkpoints have their own text that already says it is a checkpoint +// (SV's "Checkpoint:: 004 - 02 ...") passes that as `display_text`, and +// the line starts with it in place of "[Checkpoint] ": +// 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, std::string_view display_text = "" +); // 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, + std::string_view display_text = "" ); -// 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, std::string_view display_text = ""); // 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, + std::string_view display_text = "" +); -// 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 display 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 5172022a55..cdd375ed78 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" @@ -17,10 +19,9 @@ namespace Pokemon{ namespace{ // One formatter call and the line it must make. -struct StoryLogCase{ +struct StoryLogTestCase{ // A formatter's line, made when the table is built. struct Input{ - std::string name; std::string line; }; @@ -29,14 +30,15 @@ struct StoryLogCase{ std::string line; }; + 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,102 @@ 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 display 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", 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, 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, 3)}, + .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2/3"}, + }, + { + .name = "start with display text", + .input = {.line = checkpoint_start_line("004", std::nullopt, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, + }, + { + .name = "attempt with display text", + .input = {.line = checkpoint_attempt_line("004", 2, 100, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: attempt 2/100"}, + }, + { + .name = "pass with display text", + .input = {.line = checkpoint_pass_line("004", 2, SV_TEXT)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: passed on attempt 2 (1 reset)"}, + }, + { + .name = "reset with display text", + .input = {.line = checkpoint_reset_line("004", 2, std::nullopt, SV_TEXT)}, + .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 a tag", + .input = {.line = story_short_line("Checkpoint:: 004 - 02. Start point: X: start")}, + .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 +154,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_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_AutoStoryStats_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.cpp similarity index 75% rename from SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp rename to SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryRunCounts_Tests.cpp index 044c77b930..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,8 +10,9 @@ #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{ @@ -19,7 +20,7 @@ namespace Pokemon{ namespace{ // Counts recorded into the AutoStory stats and the totals they must reach. -struct StoryCountsCase{ +struct StoryCountsTestCase{ // Counts added to fresh stats, in order. struct Input{ std::string name; @@ -39,8 +40,8 @@ struct StoryCountsCase{ 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 StoryCountsCase::Expectation& 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_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 efcacab7ca..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; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h index c2b1eb594c..9ced24c3a3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_WarpJudge.h @@ -14,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_StoryCheckpoint.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h index 6a0c04702d..1f76989b29 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h @@ -14,6 +14,7 @@ #include #include #include +#include "Pokemon/Navigation/Pokemon_GateState.h" #include "Pokemon_DeclaredPose.h" #include "Pokemon_StoryCheckpointMetadata.h" #include "Pokemon_StoryPostconditions.h" @@ -39,8 +40,19 @@ struct StoryCheckpoint{ // Its phase and verification (JSON "metadata"), which decide when a // run of it may save. StoryCheckpointMetadata metadata = {}; + // Which gates are open and which switches are on at the start, where the + // save has any of the story's gates (Silph Co., Pokemon Mansion); none + // where the checkpoint does not say, which the run infers from the + // story so far, or else observes. JSON: "start_gates". + std::optional start_gates = {}; }; +// The gate state of `checkpoint` once its first `steps_done` steps are done: +// its start_gates (nothing open, no switch on if it has none) with the +// steps' gates_after applied in order. `steps_done` past the last step is +// the end. +GateState checkpoint_gates(const StoryCheckpoint& checkpoint, size_t steps_done); + } // namespace Pokemon diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp new file mode 100644 index 0000000000..2a3561835c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointGates.cpp @@ -0,0 +1,28 @@ +/* Story Checkpoint Gates + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +GateState checkpoint_gates(const StoryCheckpoint& checkpoint, size_t steps_done){ + GateState gates = checkpoint.start_gates.value_or(GateState()); + const size_t done = std::min(steps_done, checkpoint.steps.size()); + for (size_t c = 0; c < done; c++){ + if (checkpoint.steps[c].gates_after){ + apply_gate_change(gates, *checkpoint.steps[c].gates_after); + } + } + return gates; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation 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/AutoStory/Types/Pokemon_StoryStep.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h index 4500111b71..8b004bbf8c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h @@ -15,6 +15,7 @@ #include #include #include +#include "Pokemon/Navigation/Pokemon_GateState.h" #include "Pokemon_DeclaredPose.h" #include "Pokemon_StoryActionArg.h" #include "Pokemon_StoryStepKind.h" @@ -51,6 +52,10 @@ struct StoryStep{ // Where the step leaves the player, when its action does not say: the // end of a scene that moves the player. std::optional pose_after; + // The gates the step opens and the switches it sets + // (Pokemon/Navigation/Pokemon_GateState.h), applied to the run's gate + // state when the step is done. + std::optional gates_after = {}; }; diff --git a/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json new file mode 100644 index 0000000000..a7d5797eb4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_1F.json @@ -0,0 +1,63 @@ +{ + "format": "pokesim-runtime-map/2", + "name": "PokemonMansion_1F", + "source": {"decomp_file": "data/maps/PokemonMansion_1F/map.json", "revision": "ce6406d8c2eaf46d543c8a2606de0c3cb1951982", "date": "2026-10-31"}, + "tiles": [ + "######################################", + "######################################", + "######################################", + "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#", + "#~~#~#~###~~~#~~~~~#~#~#~#~~#####~~~~#", + "#~~#~~~###~~~#~~~~~#~#~#~#~~#####~~~##", + "#~~~~~~~~~~~~#~~~~~~~~~~~~~~#####~~~##", + "#~~~~~~~~~~~~#~~~~~~~~~~~~~~#~~~~~~~~#", + "#~~~~~~~~~~~~##~~~####~~~####~~~~~~~~#", + "################~~####GGG####~~~###~~#", + "################~~####GGG####~~~###~~#", + "##############~~~~#~~~~~~~####~~~~~~~#", + "#~~~~~~~~~.###~~~~#~~~~~~~~###~~~~~~~#", + "#~~~~~~~~~~###~~~~#~~~~~~~~~##~~~~~~~#", + "#~~~~~~~~~~~~~~~~~#~~~~~~~~~##~~~~~~~#", + "#~~~~~~~~~~~~~~~~~#~~~~~~~~~##~~~~~~~#", + "#~~~~~~~~~~~~~~~###############~~~####", + "#~~~~~~~~~~~~~~~###############GGG####", + "##~~~~~~~~~~~~~~###############GGG####", + "###~~#~~~~~#~~~##~~#####~~~~~~~~~~~~~#", + "###~~#~~~~~#~~~##~~~~~~#~~~~~~~~~~~~~#", + "###~~#~~~~~#~~~##~~~~~~#~~~~~~~~~~~~~#", + "###~~~~~~~~~~~~~#~~~~~~#~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~~~#~~~~~~####~~~##~~~###", + "#~~~~#~~~~~#~~~~#~~~~~~####GGG##GGG###", + "#~~~~#~~~~~#~~~~#~~########GGG##GGG###", + "#~~~~#~~~~~#~~~~#~~~~~~##.~~~~~#~~~~~#", + "#~~~~~~~~~~~~~~~#~~~~~~##~~~~~~#~~~~~#", + "#~~~~~~~~~~~~~~~#~~~~~~~~~~~~~~#~~~~~#", + "#~~~~#~~~~~#~~~~#~~~~~~~~~~~~~~#~~~~~#", + "#~~~~#~~~~~#~~~~#~~~~~~~~~~~~~~#~~~~~#", + "#~~~~#~~~~~#~~~~#~~######~~~~~~#~~~~~#", + "#~~~~~~~.~~~~~~~#~~~~~~~~~~~~~~#~~.~~#", + "######################################" + ], + "points": {"warp_1": [8, 33], "warp_3": [10, 13], "warp_4": [25, 27], "warp_7": [19, 22], "warp_8": [20, 22]}, + "warps": [ + {"enter": "walk_into", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [7, 33]}, + {"enter": "walk_south", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [8, 33]}, + {"enter": "walk_into", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [9, 33]}, + {"enter": "walk_east", "to_map": "PokemonMansion_2F", "to_point": "warp_2", "coord": [10, 13]}, + {"enter": "walk_west", "to_map": "PokemonMansion_B1F", "to_point": "warp_0", "coord": [25, 27]}, + {"enter": "walk_south", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [34, 33]}, + {"enter": "walk_into", "to_map": "CinnabarIsland", "to_point": "warp_0", "coord": [35, 34]}, + {"enter": "walk_into", "to_map": "PokemonMansion_3F", "to_point": "warp_3", "coord": [19, 22]}, + {"enter": "walk_into", "to_map": "PokemonMansion_3F", "to_point": "warp_4", "coord": [20, 22]}, + {"enter": "walk_into", "to_map": "PokemonMansion_2F", "to_point": "warp_2", "coord": [11, 13]} + ], + "connections": [], + "gates": { + "barrier_1": {"tiles": [[22, 10], [22, 11], [23, 10], [23, 11], [24, 10], [24, 11]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"}, + "barrier_2": {"tiles": [[31, 18], [31, 19], [32, 18], [32, 19], [33, 18], [33, 19]], "open_glyph": "~", "new_game": "open", "switch": "mansion", "open_when": "off"}, + "barrier_3": {"tiles": [[27, 25], [27, 26], [28, 25], [28, 26], [29, 25], [29, 26]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"}, + "barrier_4": {"tiles": [[32, 25], [32, 26], [33, 25], [33, 26], [34, 25], [34, 26]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"} + }, + "switches": {"mansion": {"flag": "FLAG_POKEMON_MANSION_SWITCH_STATE", "toggle": true, "floors": ["PokemonMansion_1F", "PokemonMansion_2F", "PokemonMansion_3F", "PokemonMansion_B1F"], "statues": [{"coord": [5, 5], "face_from": [5, 6], "facing": "north"}]}} +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json new file mode 100644 index 0000000000..8dc8b4874e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/PokemonMansion_3F.json @@ -0,0 +1,59 @@ +{ + "format": "pokesim-runtime-map/2", + "name": "PokemonMansion_3F", + "source": {"decomp_file": "data/maps/PokemonMansion_3F/map.json", "revision": "ce6406d8c2eaf46d543c8a2606de0c3cb1951982", "date": "2026-10-31"}, + "tiles": [ + "######################################", + "######################################", + "######################################", + "#~~~~~##.~~~~~~~###~~#~~~~~~~#~~~#~~~#", + "#~~~~~##~~~~~~~~~##~~#~~~~~~~#~~~~#~~#", + "##~~~~~~~~~~#~~~~##~~#~~###~~#~~~~##~#", + "###~~~~~~~~~~~~~~~~~~G~~###~~~~~~~~###", + "###~~~~~~~~~~~~~~~~~~G~~~~~~~~~~~~~~~#", + "########~######~~~~~~G~~~~~~~~~~~~~~~#", + "###############~~~~~~#~~~~~~~~~~~~~~~#", + "########~########~~~######~###~#######", + "#~###~~~~##.~~###GGG##################", + "#~~##~~~~##~~~###GGG######~~~~~#######", + "#~~#~~~~~~~~~~##~~~~~~~~~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~#~~~~~~~~~~~~~~~~~~~~~~#", + "#~~~~~~~~~~~~~####~~~##~~##~~#########", + "#~~~~~###~~~~~####~~~##~~##~~#########", + "####~~###~~~~#####~~~##~~##~~#########", + "####~~~~~~~~######~~~##~~##~~~~~##.~~#", + "#~~~~~~~~~~################~~~~~##~~~#", + "#~~~~~~~~~~~~~#############~~~~~~~~~~#", + "#~~~~~~~~~~~~~#############~~~~~~~~~~#", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################", + "######################################" + ], + "points": {"warp_0": [8, 3], "warp_1": [34, 18], "warp_2": [11, 11], "warp_3": [18, 18], "warp_4": [19, 18], "warp_5": [23, 18]}, + "warps": [ + {"enter": "walk_west", "to_map": "PokemonMansion_2F", "to_point": "warp_0", "coord": [8, 3]}, + {"enter": "walk_west", "to_map": "PokemonMansion_2F", "to_point": "warp_1", "coord": [34, 18]}, + {"enter": "walk_west", "to_map": "PokemonMansion_2F", "to_point": "warp_3", "coord": [11, 11]}, + {"enter": "step_on", "to_map": "PokemonMansion_1F", "to_point": "warp_7", "one_way": true, "coord": [18, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_1F", "to_point": "warp_8", "one_way": true, "coord": [19, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_2F", "to_point": "warp_4", "one_way": true, "coord": [23, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_1F", "to_point": "warp_8", "one_way": true, "coord": [20, 18]}, + {"enter": "step_on", "to_map": "PokemonMansion_2F", "to_point": "warp_4", "one_way": true, "coord": [24, 18]} + ], + "connections": [], + "gates": { + "barrier_1": {"tiles": [[21, 6], [21, 7], [21, 8]], "open_glyph": "~", "new_game": "open", "switch": "mansion", "open_when": "off"}, + "barrier_2": {"tiles": [[17, 11], [17, 12], [18, 11], [18, 12], [19, 11], [19, 12]], "open_glyph": "~", "new_game": "closed", "switch": "mansion", "open_when": "on"} + }, + "switches": {"mansion": {"flag": "FLAG_POKEMON_MANSION_SWITCH_STATE", "toggle": true, "floors": ["PokemonMansion_1F", "PokemonMansion_2F", "PokemonMansion_3F", "PokemonMansion_B1F"], "statues": [{"coord": [12, 5], "face_from": [12, 6], "facing": "north"}]}} +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json new file mode 100644 index 0000000000..53ecd291ad --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/GateTestMaps/SilphCo_5F.json @@ -0,0 +1,45 @@ +{ + "format": "pokesim-runtime-map/2", + "name": "SilphCo_5F", + "source": {"decomp_file": "data/maps/SilphCo_5F/map.json", "revision": "ce6406d8c2eaf46d543c8a2606de0c3cb1951982", "date": "2026-10-31"}, + "tiles": [ + "####################################", + "####################################", + "############################...#####", + "######################D#####...#####", + ".......##........###................", + ".......##...........................", + ".#.#.#.##...........................", + ".......##.........##########..#####.", + ".......##.........##########..#####.", + ".......GG..#########............###.", + ".......GG..#########.............##.", + ".#.#.#.##..##.....##.....######..##.", + ".......##..##.....##........###..##.", + "#########..###....GG........###..##.", + "#########..##.....GG........###..##.", + ".......##..##.....##.....######..##.", + "...##..##..###....##.............##.", + ".#####.##..##.....##.............##.", + "..####.GG..########################.", + "...###.GG..########################.", + "...###.##....#..#..#..#..#..#..#....", + ".......##..........................." + ], + "points": {"warp_0": [28, 2], "warp_1": [15, 7], "warp_2": [30, 2], "warp_3": [10, 20], "warp_4": [2, 20], "warp_5": [33, 5]}, + "warps": [ + {"enter": "walk_west", "to_map": "SilphCo_4F", "to_point": "warp_0", "coord": [28, 2]}, + {"enter": "step_on", "to_map": "SilphCo_3F", "to_point": "warp_1", "coord": [15, 7]}, + {"enter": "walk_east", "to_map": "SilphCo_6F", "to_point": "warp_2", "coord": [30, 2]}, + {"enter": "step_on", "to_map": "SilphCo_9F", "to_point": "warp_1", "coord": [10, 20]}, + {"enter": "step_on", "to_map": "SilphCo_3F", "to_point": "warp_5", "coord": [2, 20]}, + {"enter": "step_on", "to_map": "SilphCo_7F", "to_point": "warp_2", "coord": [33, 5]}, + {"enter": "walk_into", "to_map": "SilphCo_Elevator", "to_point": "warp_0", "coord": [22, 3]} + ], + "connections": [], + "gates": { + "door_1": {"tiles": [[7, 9], [7, 10], [8, 9], [8, 10]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_1"}, + "door_2": {"tiles": [[7, 18], [7, 19], [8, 18], [8, 19]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_2"}, + "door_3": {"tiles": [[18, 13], [18, 14], [19, 13], [19, 14]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_3"} + } +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp new file mode 100644 index 0000000000..8488e7783d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.cpp @@ -0,0 +1,67 @@ +/* Gate State + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_GateState.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +bool GateState::switch_on(const std::string& id) const{ + return switches_on.contains(id); +} + +std::string gate_key(const std::string& map, const std::string& gate_id){ + return map + "/" + gate_id; +} + +void apply_gate_change(GateState& state, const GateChange& change){ + state.opened.insert(change.opened.begin(), change.opened.end()); + for (const auto& [id, on] : change.switches){ + if (on){ + state.switches_on.insert(id); + }else{ + state.switches_on.erase(id); + } + } +} + +GateChange gate_change_between(const GateState& before, const GateState& after){ + GateChange change; + for (const std::string& key : after.opened){ + if (!before.opened.contains(key)){ + change.opened.insert(key); + } + } + for (const std::string& id : after.switches_on){ + if (!before.switch_on(id)){ + change.switches[id] = true; + } + } + for (const std::string& id : before.switches_on){ + if (!after.switch_on(id)){ + change.switches[id] = false; + } + } + return change; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +size_t std::hash::operator()(const PokemonAutomation::Pokemon::GateState& state) const{ + size_t hash = 0; + for (const std::string& key : state.opened){ + hash = hash * 31 + std::hash()(key); + } + for (const std::string& id : state.switches_on){ + hash = hash * 37 + std::hash()(id); + } + return hash; +} diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h new file mode 100644 index 0000000000..b8d007d668 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateState.h @@ -0,0 +1,76 @@ +/* Gate State + * + * From: https://github.com/PokemonAutomation/ + * + * Which of the maps' gates (MapData::gates) are open: the card-key doors + * the player has opened, and the state of each switch. A switch is shared + * by every map that lists it as a floor (Pokemon Mansion's statue + * switch), and every gate that follows it is open or closed with it. + * + * The state is plain data, with no map in it; the story keeps it + * (Pokemon_StoryCheckpoint.h). What it makes of a map's tiles is + * Pokemon_GateTiles.h, and the gated planner (Pokemon_GatedPlanner.h) + * searches over it. + * + */ + +#ifndef PokemonAutomation_Pokemon_GateState_H +#define PokemonAutomation_Pokemon_GateState_H + +#include +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Which gates are open and which switches are on. +struct GateState{ + // The card-key gates opened, each as gate_key(map, gate id). + std::set opened; + // The switches (by id) that are on. One not here is off, as at a new game. + std::set switches_on; + + bool operator==(const GateState& other) const = default; + + bool switch_on(const std::string& id) const; +}; + +// What a story step does to the state: gates it opens and switches it sets. +// What a step does to a GateState: gates it opens, switches it sets. +struct GateChange{ + std::set opened; + // The switch's new state, on or off. + std::map switches; + + bool operator==(const GateChange& other) const = default; +}; + +// Names a gate across maps: "SilphCo_5F/door_1". +std::string gate_key(const std::string& map, const std::string& gate_id); + +// Puts `change` on `state`: its gates are opened and its switches set. +void apply_gate_change(GateState& state, const GateChange& change); + +// The change that takes `before` to `after`: the gates `after` has opened +// that `before` has not, and the switches whose state differs. A card-key +// gate does not close again, so one `before` has opened and `after` has not +// is ignored. +GateChange gate_change_between(const GateState& before, const GateState& after); + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +// A hash of GateState, for planners that keep it in the search state. +template <> +struct std::hash{ + size_t operator()(const PokemonAutomation::Pokemon::GateState& state) const; +}; + +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp new file mode 100644 index 0000000000..bca7bbbdd8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.cpp @@ -0,0 +1,93 @@ +/* Gate Tiles + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_GateTiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string switch_key(const MapSwitch& pressed){ + return pressed.floors.size() == 1 ? gate_key(pressed.floors.front(), pressed.id) : pressed.id; +} + +const MapSwitch* switch_with_button(const MapData& map, const TilePos& pos){ + for (const auto& [id, result] : map.switches){ + for (const TilePos& button : result.buttons){ + if (button == pos){ + return &result; + } + } + } + return nullptr; +} + +const MapGate* gate_at(const MapData& map, const TilePos& pos){ + for (const auto& [id, gate] : map.gates){ + for (const TilePos& tile : gate.tiles){ + if (tile == pos){ + return &gate; + } + } + } + return nullptr; +} + +bool gate_is_open(const MapData& map, const MapGate& gate, const GateState& state){ + if (gate.kind == GateKind::SWITCH){ + const auto found = map.switches.find(gate.switch_id); + const std::string key = found == map.switches.end() ? gate.switch_id : switch_key(found->second); + return state.switch_on(key) == gate.open_when_on; + } + return gate.open_at_new_game || state.opened.contains(gate_key(map.name, gate.id)); +} + +TileKind resolved_tile(const MapData& map, const TilePos& pos, const GateState& state){ + const TileKind tile = map.tiles.at(pos); + if (tile != TileKind::GATE){ + return tile; + } + const MapGate* gate = gate_at(map, pos); + return gate != nullptr && gate_is_open(map, *gate, state) ? gate->open_tile : TileKind::WALL; +} + +Grid resolve_tiles(const MapData& map, const GateState& state){ + Grid grid = map.tiles; + for (int y = 0; y < grid.height; y++){ + for (int x = 0; x < grid.width; x++){ + cell_at(TilePos{x, y}, grid) = resolved_tile(map, TilePos{x, y}, state); + } + } + return grid; +} + +const MapGate* closed_card_key_gate_faced(const MapData& map, const TilePos& from, Direction direction, const GateState& state){ + const TilePos ahead = from.next(direction); + if (!map.tiles.contains(ahead) || map.tiles.at(ahead) != TileKind::GATE){ + return nullptr; + } + const MapGate* gate = gate_at(map, ahead); + if (gate == nullptr || gate->kind != GateKind::CARD_KEY || gate_is_open(map, *gate, state)){ + return nullptr; + } + return gate; +} + +const MapSwitch* switch_pressed(const MapData& map, const TilePos& from, Direction direction){ + for (const auto& [id, result] : map.switches){ + for (const MapStatue& statue : result.statues){ + if (statue.face_from == from && statue.facing == direction){ + return &result; + } + } + } + return nullptr; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h new file mode 100644 index 0000000000..6a413df2a3 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GateTiles.h @@ -0,0 +1,60 @@ +/* Gate Tiles + * + * From: https://github.com/PokemonAutomation/ + * + * What a gate state (Pokemon_GateState.h) makes of a map's tiles: a closed + * gate is a wall; an open one is the tile its `open_glyph` names. + * + */ + +#ifndef PokemonAutomation_Pokemon_GateTiles_H +#define PokemonAutomation_Pokemon_GateTiles_H + +#include +#include "Pokemon_GateState.h" +#include "Pokemon_Grid.h" +#include "Pokemon_MapData.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The name GateState::switches_on knows `pressed` by: its id when several +// maps share it (Pokemon Mansion's floors), and gate_key(its map, its id) +// when it is one map's own (each Victory Road floor has a +// "floor_switch_1" of its own, with its own flag). +std::string switch_key(const MapSwitch& pressed); + +// The switch whose boulder button is `pos`, or nullptr if no switch's is. +const MapSwitch* switch_with_button(const MapData& map, const TilePos& pos); + +// The gate that has `pos`, or nullptr if no gate does. +const MapGate* gate_at(const MapData& map, const TilePos& pos); + +// Whether `gate` of `map` is open: a card-key gate is open at a new game, +// if the map says so, or once opened; a switch's gate is open while the +// switch is in its `open_when` state. +bool gate_is_open(const MapData& map, const MapGate& gate, const GateState& state); + +// The tile at `pos` for walking: a gate tile is a wall while its gate is +// closed and its `open_glyph` tile once open; any other tile is as it is. +// `pos` must be inside the map. +TileKind resolved_tile(const MapData& map, const TilePos& pos, const GateState& state); + +// All the map's tiles, resolved. +Grid resolve_tiles(const MapData& map, const GateState& state); + +// The closed card-key gate that a player standing on `from` and facing +// `direction` faces, or nullptr if there is none (a wall, an open gate, a +// switch's gate). +const MapGate* closed_card_key_gate_faced(const MapData& map, const TilePos& from, Direction direction, const GateState& state); + +// The switch with a statue that a player standing on `from` and facing +// `direction` presses with A, or nullptr if there is none. +const MapSwitch* switch_pressed(const MapData& map, const TilePos& from, Direction direction); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h new file mode 100644 index 0000000000..1528831b52 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedPlanner.h @@ -0,0 +1,233 @@ +/* Gated Planner + * + * From: https://github.com/PokemonAutomation/ + * + * Plans legs (Pokemon_LegPlanner.h) over maps with gates (Pokemon_GateState.h). + * The game's step model walks the maps with a gate state; this wraps it, so + * the search state is the game's state and the gate state: + * + * - a closed gate is a wall, so a route goes round it; + * - a closed card-key gate the player faces can be opened (an + * ACTION_OPEN_GATE leg: face it, press A), if the Card Key is held; the + * state after the leg has it open, and the route may go through; + * - a statue faced from its tile can be pressed (an ACTION_PRESS_SWITCH + * leg), which flips the switch, so every gate that follows it changes + * with it, on every floor. A route may flip it, and flip it back. + * + * The game's step model, as for plan_legs() but taking the gate state, and + * telling where its state is: + * + * using State = ...; + * using StateHash = ...; + * std::optional> press(const State&, Direction, const GateState&) const; + * bool held_at(const State&, Direction, const GateState&) const; + * MapPlace place_of(const State&) const; + * + * and may have open_stop_risk(const State&, Direction, const GateState&) + * const and tap_stop_risk() with the same parameters, as plan_legs()'s + * model may have them without the gates. + * + * press() reads each tile with resolved_tile(), which makes a closed gate a + * wall. One-way warps (MapWarp::one_way) are the game's to take as any warp: + * the model leads the player to the warp's arrival, and the maps give no way + * back from it, so a route takes it forward only. + * + */ + +#ifndef PokemonAutomation_Pokemon_GatedPlanner_H +#define PokemonAutomation_Pokemon_GatedPlanner_H + +#include +#include +#include +#include +#include +#include "Pokemon_ConnectedMaps.h" +#include "Pokemon_GateTiles.h" +#include "Pokemon_GatedTypes.h" +#include "Pokemon_LegPlanner.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A step model that weighs an open stop (see the top of this file). +template +concept HasOpenStopRisk = requires (const Inner& inner, const typename Inner::State& state, const GateState& gates){ + { inner.open_stop_risk(state, Direction::NORTH, gates) } -> std::convertible_to; +}; + +// A step model that weighs an open stop after one step (see the top of +// this file). +template +concept HasTapStopRisk = requires (const Inner& inner, const typename Inner::State& state, const GateState& gates){ + { inner.tap_stop_risk(state, Direction::NORTH, gates) } -> std::convertible_to>; +}; + +// The game's step model, wrapped: see the top of this file. +template +class GatedModel{ +public: + using InnerState = typename Inner::State; + using State = GatedState; + + struct StateHash{ + size_t operator()(const State& state) const{ + return typename Inner::StateHash()(state.inner) * 31 + std::hash()(state.gates); + } + }; + + // `inner` and `world` must outlive the model. + GatedModel(const Inner& inner, const ConnectedMaps& world, const GateRules& rules) + : m_inner(inner) + , m_world(world) + , m_rules(rules) + {} + + std::optional> press(const State& state, Direction direction) const{ + if (std::optional> walked = m_inner.press(state.inner, direction, state.gates)){ + return carry(*walked, state.gates); + } + const MapPlace place = m_inner.place_of(state.inner); + const MapData& map = m_world.map(place.map); + if (m_rules.card_key_held){ + if (const MapGate* gate = closed_card_key_gate_faced(map, place.tile, direction, state.gates)){ + return open_gate(state, map, *gate, direction); + } + } + if (const MapSwitch* pressed = switch_pressed(map, place.tile, direction)){ + return press_switch(state, *pressed, direction); + } + return std::nullopt; + } + + bool held_at(const State& state, Direction direction) const{ + return m_inner.held_at(state.inner, direction, state.gates); + } + + // The inner model's risk of an open stop (see Pokemon_LegPlanner.h), if + // it has one, read through the gates the stop is made in. + int open_stop_risk(const State& state, Direction direction) const requires HasOpenStopRisk{ + return m_inner.open_stop_risk(state.inner, direction, state.gates); + } + std::optional tap_stop_risk(const State& state, Direction direction) const requires HasTapStopRisk{ + return m_inner.tap_stop_risk(state.inner, direction, state.gates); + } + +private: + static Step carry(const Step& walked, const GateState& gates){ + Step step; + step.next = State{walked.next, gates}; + step.action = walked.action; + step.starts_leg = walked.starts_leg; + step.ends_leg = walked.ends_leg; + step.warped = walked.warped; + step.facing = walked.facing; + step.extra_cost = walked.extra_cost; + step.wild = walked.wild; + return step; + } + + // The player stays where they are, facing the gate, which is open afterwards. + Step open_gate(const State& state, const MapData& map, const MapGate& gate, Direction direction) const{ + Step step; + step.next = state; + step.next.gates.opened.insert(gate_key(map.name, gate.id)); + step.action = ACTION_OPEN_GATE; + step.ends_leg = true; + step.facing = direction; + step.extra_cost = m_rules.open_gate_cost; + return step; + } + + Step press_switch(const State& state, const MapSwitch& pressed, Direction direction) const{ + Step step; + step.next = state; + const std::string key = switch_key(pressed); + const bool now_on = pressed.toggle ? !state.gates.switch_on(key) : true; + if (now_on){ + step.next.gates.switches_on.insert(key); + }else{ + step.next.gates.switches_on.erase(key); + } + step.action = ACTION_PRESS_SWITCH; + step.ends_leg = true; + step.facing = direction; + step.extra_cost = m_rules.press_switch_cost; + return step; + } + + const Inner& m_inner; + const ConnectedMaps& m_world; + GateRules m_rules; +}; + + +// Where a plan starts: the game's state, the gates, and the way the player faces if known. +template +struct GatedStart{ + InnerState state; + GateState gates; + std::optional facing = std::nullopt; +}; + +// Where a plan ends: a state of the game where `at_goal` holds, facing `facing` if given. +template +struct GatedGoal{ + std::function at_goal; + std::optional facing = std::nullopt; +}; + +// The cheapest legs from `start` to `goal` (as plan_legs()), or nothing. +// Each leg's start and end carry the gate state; gate_change_of() says what +// an ACTION leg did to it. +template +std::optional>>> plan_gated_legs( + const Inner& inner, const ConnectedMaps& world, const GateRules& rules, + const GatedStart& start, + const GatedGoal& goal, + const LegCosts& costs = LegCosts() +); + +// What a leg did to the gates: the gate it opened, the switch it flipped. +template +GateChange gate_change_of(const Leg>& leg); + +// The gate state after the last of `legs`, or `before` if there are none. +template +GateState gates_after(const GateState& before, const std::vector>>& legs); + + +template +std::optional>>> plan_gated_legs( + const Inner& inner, const ConnectedMaps& world, const GateRules& rules, + const GatedStart& start, + const GatedGoal& goal, + const LegCosts& costs +){ + using State = GatedState; + const GatedModel model(inner, world, rules); + return plan_legs( + model, State{start.state, start.gates}, start.facing, + std::function([&](const State& state){ + return goal.at_goal(state.inner); + }), + goal.facing, costs + ); +} + +template +GateChange gate_change_of(const Leg>& leg){ + return gate_change_between(leg.start.gates, leg.end.gates); +} + +template +GateState gates_after(const GateState& before, const std::vector>>& legs){ + return legs.empty() ? before : legs.back().end.gates; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h new file mode 100644 index 0000000000..70d1b3a4e1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_GatedTypes.h @@ -0,0 +1,49 @@ +/* Gated Types + * + * From: https://github.com/PokemonAutomation/ + * + * What the gated planner (Pokemon_GatedPlanner.h) shares with the games + * that plan with it: its actions, rules, and search state. + * + */ + +#ifndef PokemonAutomation_Pokemon_GatedTypes_H +#define PokemonAutomation_Pokemon_GatedTypes_H + +#include "Pokemon_GateState.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The Step::action of the legs the wrapper adds. A game's own actions must +// differ from these. +constexpr int ACTION_OPEN_GATE = 1000; +// See ACTION_OPEN_GATE: the leg that presses a statue. +constexpr int ACTION_PRESS_SWITCH = 1001; + +// What the story lets a plan do, and what it costs. +struct GateRules{ + // The story state says the player holds the Card Key: a plan may open card-key gates. + bool card_key_held = false; + // Cost of opening a gate or pressing a statue, on top of LegCosts::per_tile: + // the dialog, and for a statue its yes/no. + int open_gate_cost = 10; + int press_switch_cost = 20; +}; + + +// The game's state and the gate state. +template +struct GatedState{ + InnerState inner; + GateState gates; + + bool operator==(const GatedState& other) const = default; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp new file mode 100644 index 0000000000..46f062f7f6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.cpp @@ -0,0 +1,494 @@ +/* Gates Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Fixtures: GateTestMaps/, trimmed from PokemonSimulation's runtime maps + * (SilphCo_5F: card-key doors; PokemonMansion_1F and _3F: statue switch, + * barriers, holes). + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_GatedPlanner.h" +#include "Pokemon_Gates_Tests.h" +#include "Pokemon_NavigationGateTestMaps.h" + +namespace PokemonAutomation{ +namespace Pokemon{ +namespace{ + + +MapData load(const std::string& name){ + for (const auto& [file, text] : navigation_gate_test_map_json()){ + if (file == name){ + return parse_map(parse_json(text), name + ".json"); + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No gate test map " + name); +} + +// Silph 5F, Mansion 1F and 3F, in that order. +const std::vector& maps(){ + static const std::vector loaded{ + load("SilphCo_5F"), + load("PokemonMansion_1F"), + load("PokemonMansion_3F"), + }; + return loaded; +} +constexpr size_t SILPH = 0; +constexpr size_t MANSION_1F = 1; +constexpr size_t MANSION_3F = 2; + +const ConnectedMaps& world(){ + static const ConnectedMaps connected(maps()); + return connected; +} + + +// Walks the maps with the gates as the state says; a step onto a step-on +// warp takes it. Walk-into warps are not modeled: they are not the topic. +class GateGame{ +public: + using State = MapPlace; + using StateHash = std::hash; + + std::optional> press(const State& state, Direction direction, const GateState& gates) const{ + const MapData& map = world().map(state.map); + const TilePos next = state.tile.next(direction); + if (!map.tiles.contains(next) || !walkable(resolved_tile(map, next, gates))){ + return std::nullopt; + } + Step step; + step.next = {state.map, next}; + step.facing = direction; + for (const MapWarp& warp : map.warps){ + if (warp.enter != WarpEntry::STEP_ON || warp.x != next.x || warp.y != next.y){ + continue; + } + const std::optional arrival = warp.to ? world().warp_arrival(warp) : std::nullopt; + if (!arrival){ + return std::nullopt; + } + step.next = *arrival; + step.warped = true; + step.ends_leg = true; + } + return step; + } + bool held_at(const State& state, Direction direction, const GateState& gates) const{ + const MapData& map = world().map(state.map); + const TilePos next = state.tile.next(direction); + return !map.tiles.contains(next) || !walkable(resolved_tile(map, next, gates)); + } + MapPlace place_of(const State& state) const{ + return state; + } + +private: + static bool walkable(TileKind tile){ + return tile == TileKind::OPEN || tile == TileKind::TALL_GRASS; + } +}; + +using GatedLegs = std::vector>>; + +std::optional plan( + const MapPlace& from, const MapPlace& to, const GateState& gates, bool card_key_held +){ + GateRules rules; + rules.card_key_held = card_key_held; + return plan_gated_legs( + GateGame(), world(), rules, GatedStart{from, gates}, + GatedGoal{std::function([to](const MapPlace& place){ return place == to; })} + ); +} + +int count_actions(const GatedLegs& legs, int action){ + int count = 0; + for (const auto& leg : legs){ + if (leg.kind == LegKind::ACTION && leg.action == action){ + count++; + } + } + return count; +} + + +// Reader + +UnitTestResult test_reads_card_key_doors(){ + const MapData& silph = maps()[SILPH]; + if (silph.gates.size() != 3 || !silph.switches.empty()){ + return std::format("Expected 3 gates and no switches, got {} and {}.", silph.gates.size(), silph.switches.size()); + } + const auto found = silph.gates.find("door_1"); + if (found == silph.gates.end()){ + return "No door_1."; + } + const MapGate& door = found->second; + if (door.kind != GateKind::CARD_KEY || door.flag != "FLAG_SILPH_5F_DOOR_1" || door.open_at_new_game || + door.open_tile != TileKind::OPEN || door.tiles.size() != 4 + ){ + return "door_1 is not a closed card-key door of 4 tiles opening to floor with its flag."; + } + if (silph.tiles.at(TilePos{7, 9}) != TileKind::GATE){ + return "Tile (7, 9) is not a gate."; + } + return true; +} + +UnitTestResult test_reads_switches(){ + const MapData& first = maps()[MANSION_1F]; + if (first.gates.size() != 4 || first.switches.size() != 1){ + return std::format("Expected 4 gates and 1 switch, got {} and {}.", first.gates.size(), first.switches.size()); + } + const MapGate& open_first = first.gates.at("barrier_2"); + if (open_first.kind != GateKind::SWITCH || open_first.switch_id != "mansion" || open_first.open_when_on || !open_first.open_at_new_game){ + return "1F barrier_2 should open when the switch is off."; + } + if (!first.gates.at("barrier_1").open_when_on){ + return "1F barrier_1 should open when the switch is on."; + } + const MapSwitch& pressing = first.switches.at("mansion"); + if (!pressing.toggle || pressing.floors.size() != 4 || pressing.flag != "FLAG_POKEMON_MANSION_SWITCH_STATE"){ + return "The switch is a toggle on 4 floors with its flag."; + } + if (pressing.statues.size() != 1 || pressing.statues[0].tile != TilePos{5, 5} || + pressing.statues[0].face_from != TilePos{5, 6} || pressing.statues[0].facing != Direction::NORTH + ){ + return "1F statue should be (5, 5), pressed from (5, 6) facing north."; + } + return true; +} + +UnitTestResult test_reads_one_way_warps(){ + int drops = 0; + for (const MapWarp& warp : maps()[MANSION_3F].warps){ + drops += warp.one_way ? 1 : 0; + } + if (drops != 5){ + return std::format("Expected 5 one-way warps on 3F, got {}.", drops); + } + for (const MapWarp& warp : maps()[MANSION_1F].warps){ + if (warp.one_way){ + return "A 1F warp is one-way."; + } + } + return true; +} + +// A 3x3 map with `gates` and `switches` (JSON object text) on tiles "G..". +std::string parse_error(const std::string& rows, const std::string& gates, const std::string& switches){ + const std::string text = std::format(R"({{ + "format": "pokesim-runtime-map/2", "name": "T", + "source": {{"decomp_file": "x", "revision": "y", "date": "z"}}, + "tiles": [{}], "points": {{}}, "warps": [], "connections": [], + "gates": {}, "switches": {} + }})", rows, gates, switches); + try{ + parse_map(parse_json(text), "T.json"); + }catch (ParseException& e){ + return e.message(); + } + return ""; +} + +UnitTestResult test_gate_errors(){ + const std::string rows = R"("G..", "...", "...")"; + const std::string door = R"({"d": {"tiles": [[0, 0]], "open_glyph": ".", "new_game": "closed", "opens_by": "card_key_face_a", "flag": "F"}})"; + if (!parse_error(rows, door, "{}").empty()){ + return "A valid gate failed to parse."; + } + if (parse_error(rows, "{}", "{}").empty()){ + return "A G tile in no gate was accepted."; + } + if (parse_error(R"("...", "...", "...")", door, "{}").empty()){ + return "A gate on a tile that is not G was accepted."; + } + const std::string unknown_switch = R"({"d": {"tiles": [[0, 0]], "open_glyph": ".", "new_game": "closed", "switch": "nope", "open_when": "on"}})"; + if (parse_error(rows, unknown_switch, "{}").empty()){ + return "A gate on an unlisted switch was accepted."; + } + const std::string far_statue = R"({"s": {"flag": "F", "toggle": true, "floors": ["T"], "statues": [{"coord": [9, 9], "face_from": [1, 1], "facing": "north"}]}})"; + if (parse_error(rows, door, far_statue).empty()){ + return "A statue outside the map was accepted."; + } + return true; +} + + +// State and tiles + +UnitTestResult test_tiles_follow_state(){ + const MapData& silph = maps()[SILPH]; + GateState state; + if (resolved_tile(silph, TilePos{7, 9}, state) != TileKind::WALL){ + return "A closed door is not a wall."; + } + state.opened.insert(gate_key("SilphCo_5F", "door_1")); + if (resolved_tile(silph, TilePos{7, 9}, state) != TileKind::OPEN || resolved_tile(silph, TilePos{7, 18}, state) != TileKind::WALL){ + return "Only door_1 should be open."; + } + if (resolve_tiles(silph, state).at(TilePos{8, 10}) != TileKind::OPEN){ + return "resolve_tiles disagrees."; + } + + // The Mansion's gates follow the one switch on every floor. + const MapData& first = maps()[MANSION_1F]; + const MapData& third = maps()[MANSION_3F]; + GateState off; + GateState on; + on.switches_on.insert("mansion"); + if (resolved_tile(first, TilePos{22, 10}, off) != TileKind::WALL || resolved_tile(first, TilePos{22, 10}, on) != TileKind::TALL_GRASS){ + return "1F barrier_1 should open with the switch on."; + } + if (resolved_tile(third, TilePos{21, 6}, off) != TileKind::TALL_GRASS || resolved_tile(third, TilePos{21, 6}, on) != TileKind::WALL){ + return "3F barrier_1 should close with the switch on."; + } + return true; +} + +UnitTestResult test_gate_change(){ + GateState state; + GateChange change; + change.opened.insert("SilphCo_5F/door_1"); + change.switches["mansion"] = true; + apply_gate_change(state, change); + if (!state.opened.contains("SilphCo_5F/door_1") || !state.switch_on("mansion")){ + return "The change was not applied."; + } + GateChange off; + off.switches["mansion"] = false; + GateState after = state; + apply_gate_change(after, off); + if (after.switch_on("mansion") || !after.opened.contains("SilphCo_5F/door_1")){ + return "Turning the switch off changed more, or nothing."; + } + GateState rebuilt; + apply_gate_change(rebuilt, gate_change_between(GateState(), state)); + if (!(rebuilt == state)){ + return "gate_change_between from nothing does not rebuild the state."; + } + apply_gate_change(rebuilt, gate_change_between(state, after)); + if (!(rebuilt == after)){ + return "gate_change_between does not carry state to state."; + } + const GateState copy = after; + if (std::hash()(copy) != std::hash()(rebuilt)){ + return "Equal states hash differently."; + } + return true; +} + + +// Planner + +UnitTestResult test_silph_door_needs_key(){ + const MapPlace from{SILPH, {10, 9}}; + const MapPlace to{SILPH, {5, 9}}; + if (plan(from, to, GateState(), false)){ + return "Planned into the room without the Card Key."; + } + const auto legs = plan(from, to, GateState(), true); + if (!legs){ + return "No route with the Card Key."; + } + if (count_actions(*legs, ACTION_OPEN_GATE) != 1){ + return std::format("Expected one open-gate leg, got {}.", count_actions(*legs, ACTION_OPEN_GATE)); + } + GateChange expected; + expected.opened.insert("SilphCo_5F/door_1"); + for (const auto& leg : *legs){ + if (leg.kind == LegKind::ACTION){ + if (!(gate_change_of(leg) == expected)){ + return "The open-gate leg did not open door_1 alone."; + } + if (leg.direction != Direction::WEST || !(leg.start.inner == MapPlace{SILPH, {9, 9}})){ + return "Expected to open it facing west from (9, 9)."; + } + } + } + if (!gates_after(GateState(), *legs).opened.contains("SilphCo_5F/door_1")){ + return "The state after the plan does not have door_1 open."; + } + return true; +} + +UnitTestResult test_silph_open_door_needs_no_key(){ + GateState opened; + opened.opened.insert(gate_key("SilphCo_5F", "door_1")); + const auto legs = plan({SILPH, {10, 9}}, {SILPH, {5, 9}}, opened, false); + if (!legs){ + return "No route through an open door."; + } + if (count_actions(*legs, ACTION_OPEN_GATE) != 0){ + return "Opened a door that was open."; + } + return true; +} + +UnitTestResult test_mansion_press_opens_barrier(){ + // (19, 13) lies behind barrier_1 and barrier_2 of 1F, which open with the switch on. + const MapPlace from{MANSION_1F, {5, 8}}; + const MapPlace to{MANSION_1F, {19, 13}}; + const auto legs = plan(from, to, GateState(), false); + if (!legs){ + return "No route."; + } + if (count_actions(*legs, ACTION_PRESS_SWITCH) != 1){ + return std::format("Expected one statue press, got {}.", count_actions(*legs, ACTION_PRESS_SWITCH)); + } + if (!gates_after(GateState(), *legs).switch_on("mansion")){ + return "The switch is not on after the plan."; + } + for (const auto& leg : *legs){ + if (leg.kind == LegKind::ACTION && (leg.direction != Direction::NORTH || !(leg.start.inner == MapPlace{MANSION_1F, {5, 6}}))){ + return "Expected to press the statue from (5, 6) facing north."; + } + } + return true; +} + +UnitTestResult test_mansion_press_again(){ + // Switch already on; (25, 21) is reachable only with it off. + GateState on; + on.switches_on.insert("mansion"); + const auto legs = plan({MANSION_1F, {5, 6}}, {MANSION_1F, {25, 21}}, on, false); + if (!legs){ + return "No route."; + } + if (count_actions(*legs, ACTION_PRESS_SWITCH) != 1 || gates_after(on, *legs).switch_on("mansion")){ + return "Expected one press, ending with the switch off."; + } + return true; +} + +UnitTestResult test_mansion_no_press_needed(){ + // Reachable with the switch as it is: no press. + const auto legs = plan({MANSION_1F, {5, 6}}, {MANSION_1F, {5, 8}}, GateState(), false); + if (!legs || count_actions(*legs, ACTION_PRESS_SWITCH) != 0){ + return "Pressed a statue that was not needed."; + } + return true; +} + +UnitTestResult test_hole_one_way(){ + // The 3F hole at (18, 18) drops to 1F's (19, 22); nothing leads back up. + const auto down = plan({MANSION_3F, {18, 17}}, {MANSION_1F, {19, 22}}, GateState(), false); + if (!down){ + return "No route down the hole."; + } + bool warped = false; + for (const auto& leg : *down){ + warped = warped || leg.warps; + } + if (!warped){ + return "The route does not take the warp."; + } + if (plan({MANSION_1F, {19, 22}}, {MANSION_3F, {18, 17}}, GateState(), false)){ + return "Planned back up a hole."; + } + return true; +} + + +struct GatesTestCase{ + struct Input{ + std::string name; + UnitTestResult (*test)(); + }; + + struct Expectation{ + std::string_view description; + }; + + Input input; + Expectation expectation; +}; + +class Test_Gates : public UnitTest{ +public: + explicit Test_Gates(GatesTestCase scenario) + : UnitTest(std::format("Pokemon::Gates - {}", scenario.input.name)) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_scenario.input.test(); + } + +private: + GatesTestCase m_scenario; +}; + +const GatesTestCase GATES_CASES[] = { + { + .input = {.name = "reads card-key doors", .test = test_reads_card_key_doors}, + .expectation = {.description = "Silph 5F doors parse with tiles, flag, open glyph"}, + }, + { + .input = {.name = "reads switches and statues", .test = test_reads_switches}, + .expectation = {.description = "Mansion gates follow the switch; statue tile and press spot"}, + }, + { + .input = {.name = "reads one-way warps", .test = test_reads_one_way_warps}, + .expectation = {.description = "3F holes are one-way"}, + }, + { + .input = {.name = "gate errors", .test = test_gate_errors}, + .expectation = {.description = "bad gates fail to parse"}, + }, + { + .input = {.name = "tiles follow the state", .test = test_tiles_follow_state}, + .expectation = {.description = "closed gate is a wall, open is its glyph, Mansion follows the switch"}, + }, + { + .input = {.name = "gate change", .test = test_gate_change}, + .expectation = {.description = "apply and between agree"}, + }, + { + .input = {.name = "a card-key door needs the key", .test = test_silph_door_needs_key}, + .expectation = {.description = "open-gate leg only with the Card Key held"}, + }, + { + .input = {.name = "an open door needs no key", .test = test_silph_open_door_needs_no_key}, + .expectation = {.description = "no open-gate leg when the state has it open"}, + }, + { + .input = {.name = "a statue press opens a barrier", .test = test_mansion_press_opens_barrier}, + .expectation = {.description = "one press-switch leg; switch on afterwards"}, + }, + { + .input = {.name = "a press again closes it", .test = test_mansion_press_again}, + .expectation = {.description = "switch is part of the search state"}, + }, + { + .input = {.name = "no press when not needed", .test = test_mansion_no_press_needed}, + .expectation = {.description = "does not toggle without reason"}, + }, + { + .input = {.name = "a hole is one-way", .test = test_hole_one_way}, + .expectation = {.description = "down yes, back up no"}, + }, +}; + +} // namespace + + +void add_tests_Gates(UnitTestDatabase& database){ + for (const auto& scenario : GATES_CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h new file mode 100644 index 0000000000..e82848786c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Gates_Tests.h @@ -0,0 +1,24 @@ +/* Gates Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_Gates_Tests_H +#define PokemonAutomation_Pokemon_Gates_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the gate tests: the map reader's gates, switches and one-way +// warps, the gate state, and the gated planner. +void add_tests_Gates(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif 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_MapData.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp index 6fde755d24..e22233548a 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.cpp @@ -36,6 +36,7 @@ constexpr std::pair GLYPHS[] = { {'2', TileKind::CLOSED_SOUTH}, {'4', TileKind::CLOSED_WEST}, {'6', TileKind::CLOSED_EAST}, + {'G', TileKind::GATE}, }; constexpr std::pair WARP_ENTRIES[] = { @@ -361,6 +362,7 @@ MapWarp read_warp(const JsonObject& object, const std::string& where){ warp.x = pos.x; warp.y = pos.y; warp.enter = read_warp_entry(object, where); + warp.one_way = optional_bool(object, "one_way", false, where); const bool has_map = object.get_value("to_map") != nullptr; const bool has_point = object.get_value("to_point") != nullptr; if (has_map != has_point){ @@ -480,6 +482,131 @@ MapPatch read_patch(const JsonObject& object, const std::string& where){ return patch; } +// The tiles of a gate or a coordinate pair, `[x, y]` each. +std::vector read_pair_list(const JsonObject& object, const std::string& key, const std::string& where){ + const JsonArray* array = optional_array(object, key, where); + if (array == nullptr || array->empty()){ + throw ParseException(std::format("{}{} is not a non-empty list of [x, y].", where, key)); + } + std::vector pairs; + for (size_t c = 0; c < array->size(); c++){ + pairs.push_back(read_pair((*array)[c], std::format("{}{}[{}]", where, key, c))); + } + return pairs; +} + +TilePos read_pair_field(const JsonObject& object, const std::string& key, const std::string& where){ + return read_pair(read_field(object, key, where), std::format("{}{}", where, key)); +} + +bool read_on_off(const JsonObject& object, const std::string& key, const std::string& where){ + const std::string text = read_string(object, key, where); + if (text != "on" && text != "off"){ + throw ParseException(std::format("{}{} \"{}\" is not on or off.", where, key, text)); + } + return text == "on"; +} + +// A gate is a card-key door (opens_by and flag) or follows a switch (switch +// and open_when); never both. +MapGate read_gate(const std::string& id, const JsonObject& object, const std::string& where){ + MapGate gate; + gate.id = id; + gate.tiles = read_pair_list(object, "tiles", where); + const std::string glyph = read_string(object, "open_glyph", where); + const std::optional open = glyph.size() == 1 ? tile_from_glyph(glyph[0]) : std::nullopt; + if (!open || *open == TileKind::GATE){ + throw ParseException(std::format("{}open_glyph \"{}\" is not a tile glyph other than G.", where, glyph)); + } + gate.open_tile = *open; + const std::string new_game = read_string(object, "new_game", where); + if (new_game != "open" && new_game != "closed"){ + throw ParseException(std::format("{}new_game \"{}\" is not open or closed.", where, new_game)); + } + gate.open_at_new_game = new_game == "open"; + const bool by_key = object.get_value("opens_by") != nullptr || object.get_value("flag") != nullptr; + const bool by_switch = object.get_value("switch") != nullptr || object.get_value("open_when") != nullptr; + if (by_key == by_switch){ + throw ParseException(std::format("{}needs opens_by and flag, or switch and open_when, and not both.", where)); + } + if (by_key){ + const std::string opens_by = read_string(object, "opens_by", where); + if (opens_by != "card_key_face_a"){ + throw ParseException(std::format("{}opens_by \"{}\" is not card_key_face_a.", where, opens_by)); + } + gate.kind = GateKind::CARD_KEY; + gate.flag = read_string(object, "flag", where); + }else{ + gate.kind = GateKind::SWITCH; + gate.switch_id = read_string(object, "switch", where); + gate.open_when_on = read_on_off(object, "open_when", where); + } + return gate; +} + +std::map read_gates(const JsonObject& root, const std::string& where){ + std::map gates; + const JsonObject* object = optional_object(root, "gates", where); + if (object == nullptr){ + return gates; + } + for (const auto& [id, value] : *object){ + const std::string gate_where = std::format("{}gates.{}", where, id); + gates[id] = read_gate(id, as_object(value, gate_where), gate_where + "."); + } + return gates; +} + +MapStatue read_statue(const JsonObject& object, const std::string& where){ + MapStatue statue; + statue.tile = read_pair_field(object, "coord", where); + statue.face_from = read_pair_field(object, "face_from", where); + statue.facing = read_direction(object, "facing", where); + return statue; +} + +TilePos read_button(const JsonObject& object, const std::string& where){ + return read_pair_field(object, "coord", where); +} + +MapSwitch read_switch(const std::string& id, const JsonObject& object, const std::string& where){ + MapSwitch result; + result.id = id; + result.flag = read_string(object, "flag", where); + const JsonValue* toggle = object.get_value("toggle"); + if (toggle != nullptr && !toggle->is_boolean()){ + throw ParseException(std::format("{}toggle is not true or false.", where)); + } + result.toggle = toggle == nullptr || toggle->to_boolean_default(true); + const JsonArray* floors = optional_array(object, "floors", where); + if (floors == nullptr || floors->empty()){ + throw ParseException(std::format("{}floors is not a non-empty list of map names.", where)); + } + for (size_t c = 0; c < floors->size(); c++){ + const std::string* floor = (*floors)[c].to_string(); + if (floor == nullptr){ + throw ParseException(std::format("{}floors[{}] is not a string.", where, c)); + } + result.floors.push_back(*floor); + } + result.statues = read_list(object, "statues", where, read_statue); + result.buttons = read_list(object, "buttons", where, read_button); + return result; +} + +std::map read_switches(const JsonObject& root, const std::string& where){ + std::map switches; + const JsonObject* object = optional_object(root, "switches", where); + if (object == nullptr){ + return switches; + } + for (const auto& [id, value] : *object){ + const std::string switch_where = std::format("{}switches.{}", where, id); + switches[id] = read_switch(id, as_object(value, switch_where), switch_where + "."); + } + return switches; +} + void check_inside(const Grid& tiles, const TilePos& pos, const std::string& where){ if (!tiles.contains(pos)){ @@ -520,6 +647,45 @@ void check_places(const MapData& map, const std::string& where){ } } +// Every gate tile is a G tile, in one gate only; every G tile is in a +// gate; a gate follows a switch the map lists; a statue stands on a wall +// and is faced from a tile inside the map. +void check_gates(const MapData& map, const std::string& where){ + std::set in_gate; + for (const auto& [id, gate] : map.gates){ + for (size_t c = 0; c < gate.tiles.size(); c++){ + const std::string tile_where = std::format("{}gates.{}.tiles[{}]", where, id, c); + check_inside(map.tiles, gate.tiles[c], tile_where); + if (map.tiles.at(gate.tiles[c]) != TileKind::GATE){ + throw ParseException(std::format("{} {} is not a G tile.", tile_where, to_string(gate.tiles[c]))); + } + if (!in_gate.insert(gate.tiles[c]).second){ + throw ParseException(std::format("{} {} is in another gate too.", tile_where, to_string(gate.tiles[c]))); + } + } + if (gate.kind == GateKind::SWITCH && !map.switches.contains(gate.switch_id)){ + throw ParseException(std::format("{}gates.{}.switch \"{}\" is not in switches.", where, id, gate.switch_id)); + } + } + for (int y = 0; y < map.tiles.height; y++){ + for (int x = 0; x < map.tiles.width; x++){ + if (map.tiles.at({x, y}) == TileKind::GATE && !in_gate.contains(TilePos{x, y})){ + throw ParseException(std::format("{}tiles has a G at {} that no gate lists.", where, to_string(TilePos{x, y}))); + } + } + } + for (const auto& [id, result] : map.switches){ + for (size_t c = 0; c < result.statues.size(); c++){ + const std::string statue_where = std::format("{}switches.{}.statues[{}]", where, id, c); + check_inside(map.tiles, result.statues[c].tile, statue_where); + check_inside(map.tiles, result.statues[c].face_from, statue_where); + } + for (size_t c = 0; c < result.buttons.size(); c++){ + check_inside(map.tiles, result.buttons[c], std::format("{}switches.{}.buttons[{}]", where, id, c)); + } + } +} + } // namespace @@ -542,10 +708,13 @@ MapData parse_map(const JsonValue& json, const std::string& source_name){ map.flag_sets = read_flag_sets(root, where); map.connections = read_list(root, "connections", where, read_connection); map.patches = read_list(root, "patches", where, read_patch); + map.gates = read_gates(root, where); + map.switches = read_switches(root, where); check_places(map, where); for (const MapPatch& patch : map.patches){ cell_at(TilePos{patch.x, patch.y}, map.tiles) = patch.glyph; } + check_gates(map, where); return map; } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h index 6b4d923fbf..35d485e577 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapData.h @@ -26,9 +26,19 @@ * "triggers": [{"name": "mom_warning", "box": {"x0": 1, "y0": 2, "x1": 1, "y1": 2}, "when": "00.01"}, ...], * "flag_sets": {"after_00.01": ["FLAG_HIDE_MOM", ...], ...}, * "connections": [{"side": "east", "to_map": "Route 1", "offset": 0}, ...], - * "patches": [{"x": 2, "y": 1, "glyph": "#", "measured_by": "run 3, 2026-10-30"}, ...] + * "patches": [{"x": 2, "y": 1, "glyph": "#", "measured_by": "run 3, 2026-10-30"}, ...], + * "gates": {"door_1": {"tiles": [[7, 9], [7, 10]], "open_glyph": ".", "new_game": "closed", + * "opens_by": "card_key_face_a", "flag": "FLAG_SILPH_5F_DOOR_1"}, + * "barrier_1": {"tiles": [[22, 10]], "open_glyph": "~", "new_game": "closed", + * "switch": "mansion", "open_when": "on"}, ...}, + * "switches": {"mansion": {"flag": "FLAG_POKEMON_MANSION_SWITCH_STATE", "toggle": true, + * "floors": ["PokemonMansion_1F", ...], + * "statues": [{"coord": [5, 5], "face_from": [5, 6], "facing": "north"}]}} * } * + * A warp may carry "one_way": true (a drop). A map's tiles show each gate + * tile as 'G', and every 'G' tile is in a gate. + * * `name`, `source` and `tiles` are required; everything else may be left * out. Which game's planner reads these maps, and how, is the game's * business. @@ -47,6 +57,7 @@ #include "Common/Cpp/Json/JsonValue.h" #include "Pokemon_Grid.h" #include "Pokemon_MapFeatures.h" +#include "Pokemon_MapGates.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -76,6 +87,12 @@ struct MapData{ std::map> flag_sets; std::vector connections; std::vector patches; + // The barriers the story opens, by id. Every GATE tile of `tiles` is in + // exactly one gate. + std::map gates; + // The switches (by id) that gates of this map follow or that have a + // statue on it. + std::map switches; }; // Reads legacy/unmarked v1 or compact pokesim-runtime-map/2 JSON. diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h index b39d50bbf7..fd44902707 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapFeatures.h @@ -31,6 +31,9 @@ struct MapWarp{ int y = 0; WarpEntry enter = WarpEntry::WALK_INTO; std::optional to; + // A drop (a hole the player falls through): the landing has no way back + // up, so a route takes it forward only. + bool one_way = false; }; // Type classification for interactive map objects. diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h new file mode 100644 index 0000000000..11f902b09f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapGates.h @@ -0,0 +1,73 @@ +/* Map Gates + * + * From: https://github.com/PokemonAutomation/ + * + * The barriers a map's story opens (card-key doors, switch-driven walls) and + * the statues that press the switches. Pokemon_MapData.h holds them in a + * map's `gates` and `switches`; Pokemon_GateState.h says which are open. + * + */ + +#ifndef PokemonAutomation_Pokemon_MapGates_H +#define PokemonAutomation_Pokemon_MapGates_H + +#include +#include +#include "Pokemon_Grid.h" +#include "Pokemon_MapTiles.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What opens a gate. +enum class GateKind{ + CARD_KEY, // "card_key_face_a": faced from a side tile and opened with A, once the player holds the Card Key + SWITCH, // a switch's state (MapSwitch): open while the switch is `open_when_on` +}; + +// A barrier the story opens or closes: some tiles of the map that are `G` +// in `tiles`. Open, they read as `open_tile`; closed, as a wall. +struct MapGate{ + std::string id; + std::vector tiles; + TileKind open_tile = TileKind::OPEN; + // Whether a new game has it open. + bool open_at_new_game = false; + GateKind kind = GateKind::CARD_KEY; + // CARD_KEY: the event flag the game sets once it is opened. + std::string flag = {}; + // SWITCH: the switch (a key of MapData::switches, shared by the + // maps in its `floors`), and the state in which this gate is open. + std::string switch_id = {}; + bool open_when_on = false; +}; + +// A statue that presses a switch: the player stands on `face_from`, faces +// `facing` and presses A. The statue is the wall tile `tile`. +struct MapStatue{ + TilePos tile; + TilePos face_from; + Direction facing = Direction::NORTH; +}; + +// A switch the map's gates follow. One state is shared by every map in +// `floors` (Pokemon Mansion's statue switch, an event flag): pressing a +// statue flips it, and each floor redraws its gates when it loads. +// `statues` are the statues on this map only. `buttons` are the tiles a +// Strength boulder turns it on from (Victory Road's floor switches, which +// stay on). +struct MapSwitch{ + std::string id; + std::string flag; + bool toggle = true; + std::vector floors; + std::vector statues; + std::vector buttons = {}; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h index 63ebf79735..d795966527 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_MapTiles.h @@ -40,6 +40,10 @@ enum class TileKind : uint8_t{ CLOSED_SOUTH, // '2' not entered from it, nor left through it (the CLOSED_WEST, // '4' number pad's arrows) CLOSED_EAST, // '6' + GATE, // 'G' a wall until the story opens it: the map's `gates` + // table names the gate, and Pokemon_GateState.h says + // whether it is open (a reader that ignores gates + // treats it as a wall) }; // The tile a glyph stands for, or nothing if it isn't one. std::optional tile_from_glyph(char glyph); diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h new file mode 100644 index 0000000000..2b3503fee4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_NavigationGateTestMaps.h @@ -0,0 +1,30 @@ +/* Navigation Gate Test Maps + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_NavigationGateTestMaps_H +#define PokemonAutomation_Pokemon_NavigationGateTestMaps_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// (name, JSON text) of each map in Pokemon/Navigation/GateTestMaps/, built +// into the program by pa_embed_text_files() (cmake/EmbedTextFiles.cmake): +// SilphCo_5F (card-key doors), PokemonMansion_1F (statue-switch barriers) +// and PokemonMansion_3F (barriers, a statue, and holes: one-way warps), +// trimmed from PokemonSimulation's runtime maps. 3F's hole tiles are +// tall grass here, which the reader knows. +const std::vector>& navigation_gate_test_map_json(); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp index 19ac59cf41..39889a01c1 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog.cpp @@ -33,6 +33,10 @@ std::string to_string(const RouteLogLeg& leg){ return std::format("surf {}", direction); case RouteLegAction::FACE: return std::format("face {}", direction); + case RouteLegAction::OPEN_GATE: + return std::format("open gate {}", direction); + case RouteLegAction::PRESS_SWITCH: + return std::format("press switch {}", direction); case RouteLegAction::WALK: break; } diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h index cbdd909b62..34258fc7e0 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLogTypes.h @@ -38,6 +38,10 @@ enum class RouteLegAction{ WALK, SURF, FACE, + // Face a gate and press A: a card-key gate opens. + OPEN_GATE, + // Face a statue and press A, then Yes: a switch flips. + PRESS_SWITCH, }; // How a leg ends: at a wall, on a warp, or where it is timed 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/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