Merge co-op, off by default: rooms through a relay, progress sync between the players' own saves, and a Co-op tab.

Without the other players drawn in the world, which are unfinished. No relay address is built in, and with co-op off nothing opens a socket. Built on Windows and tested against a relay on this PC only; two games in one room and any real network have not been tried.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-11 00:40:20 -05:00
63 changed files with 20726 additions and 50 deletions

View File

@@ -36,6 +36,7 @@ static int g_font_bad;
#pragma clang diagnostic pop
#include "ui/amiibo_page.h"
#include "ui/binds_page.h"
#include "ui/coop_page.h"
#include "ui/option_rows.h"
#include "ui/perf_panel.h"
@@ -139,7 +140,7 @@ extern "C" void menu_toggle(void) {
}
// ---- events from the window adapter, queued while the menu is open; the present path hands them to ImGui
enum { EV_KEY, EV_POS, EV_BUTTON, EV_WHEEL, EV_FOCUS, EV_MAX = 256 };
enum { EV_KEY, EV_POS, EV_BUTTON, EV_WHEEL, EV_FOCUS, EV_CHAR, EV_MAX = 256 };
struct Ev { int type, a, b; float x, y; };
static Ev ev_q[EV_MAX];
@@ -162,6 +163,28 @@ extern "C" void menu_event_mouse_pos(float x, float y) { push({EV_POS, 0, 0, x,
extern "C" void menu_event_mouse_button(int button, int down) { push({EV_BUTTON, button, down, 0, 0}); }
extern "C" void menu_event_wheel(float dx, float dy) { push({EV_WHEEL, 0, 0, dx, dy}); }
extern "C" void menu_event_focus(int focused) { push({EV_FOCUS, focused, 0, 0, 0}); }
extern "C" void menu_event_char(unsigned c) { push({EV_CHAR, (int)c, 0, 0, 0}); }
// The clipboard's text a page asked for (gfx_window_paste): the window thread reads it, the page takes it a frame later.
static PlatMutex paste_lock = PLAT_MUTEX_INIT;
static char paste_text[256];
static int paste_ready;
extern "C" void menu_event_paste(const char *utf8) {
plat_mutex_lock(&paste_lock);
snprintf(paste_text, sizeof paste_text, "%s", utf8 ? utf8 : "");
paste_ready = 1;
plat_mutex_unlock(&paste_lock);
}
extern "C" int menu_paste_take(char *out, size_t cap) {
plat_mutex_lock(&paste_lock);
int ready = paste_ready;
if (ready && cap) snprintf(out, cap, "%s", paste_text);
paste_ready = 0;
plat_mutex_unlock(&paste_lock);
return ready;
}
static void drain_events(ImGuiIO &io) {
Ev q[EV_MAX];
@@ -183,6 +206,7 @@ static void drain_events(ImGuiIO &io) {
case EV_BUTTON: io.AddMouseButtonEvent(e.a, e.b != 0); break;
case EV_WHEEL: io.AddMouseWheelEvent(e.x, e.y); break;
case EV_FOCUS: io.AddFocusEvent(e.a != 0); break;
case EV_CHAR: io.AddInputCharacter((unsigned)e.a); break;
}
}
}
@@ -452,7 +476,7 @@ static int fonts_up(float scale) {
// what the title face shows in the CJK languages: the menu's title and tabs (Simplified Chinese menu text) and
// the boot screen's title (the game's language)
static const UiTextId titles[] = {UI_MENU_TITLE, UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_SOUND,
UI_MENU_TAB_CONTROLS, UI_BOOT_TITLE};
UI_MENU_TAB_CONTROLS, UI_MENU_TAB_COOP, UI_BOOT_TITLE};
for (int i = 0; i < N; i++) {
if (!data[i]) continue;
ImFontGlyphRangesBuilder b;
@@ -509,34 +533,44 @@ extern "C" void menu_vk_release(void) {
}
// ---- test hooks
// NX2NAVI_MENU_TEST=<frame>[:<frames per page>]: at that presented frame the menu opens on its first tab; each tab stays
// <frames per page> frames (default 30) and its last frame's swapchain image goes to NX2NAVI_MENU_TEST_DIR (default: the
// working directory) as menu-test-<frame>-<page>.png; after the last tab the menu closes.
// NX2NAVI_MENU_TEST=<frame>[,<frame>...][:<frames per page>]: at each such presented frame (up to 8) the menu opens on
// its first tab; each tab stays <frames per page> frames (default 30) and its last frame's swapchain image goes to
// NX2NAVI_MENU_TEST_DIR (default: the working directory) as menu-test-<frame>-<page>.png; after the last tab the menu
// closes.
// NX2NAVI_MENU_TEST_CLICK=<key>:<part>[,...]: mouse clicks on the rows of the page shown (ui/option_rows.cpp click_test).
// NX2NAVI_MENU_TEST_SET=<frame>:<key>=<value>[,<key>=<value>...][;<frame>:...]: cfg_set of each key (NX2NAVI_ prefix
// optional) at that presented frame; changes live settings with no input, menu open or not. nx2navi.ini is written only by
// the pseudo-key SAVE=1 (cfg_save, as a row change does; a failed write shows the menu's save note). FOCUS=<key> (the
// NX2NAVI_ prefix optional) puts the cursor on that setting's row and scrolls it into view when the row is next drawn
// (opt_focus_key), so a page's shot shows a row below the first screenful; it does not switch tabs.
enum { TAB_SCREEN, TAB_GAME, TAB_SOUND, TAB_CONTROLS, TAB_COUNT }; // Ocarina of Time: the options this port has
enum { TEST_PAGE_FRAMES = 30, TEST_SETS = 16 };
// (opt_focus_key), so a page's shot shows a row below the first screenful; it does not switch tabs. COOP_PAGE, COOP_JOIN
// and COOP_ACCEPT_OFFER drive the Co-op tab (ui/coop_page.h coop_test_set).
// Ocarina of Time: the options this port has and co-op (ui/coop_page.cpp)
enum { TAB_SCREEN, TAB_GAME, TAB_SOUND, TAB_CONTROLS, TAB_COOP, TAB_COUNT };
enum { TEST_PAGE_FRAMES = 30, TEST_SETS = 16, TEST_OPENS = 8 };
struct TestSet { uint64_t frame; char key[64], value[64]; };
static struct {
int parsed;
uint64_t at; // NX2NAVI_MENU_TEST frame; 0 none
uint64_t at[TEST_OPENS]; // NX2NAVI_MENU_TEST frames, in order
int n_at, next_at; // how many; the next to open at
uint64_t per_page;
uint64_t opened; // the frame the hook opened the menu; 0 not yet
int page; // the tab being shot; -1 none
TestSet sets[TEST_SETS];
int n_sets, next_set; // NX2NAVI_MENU_TEST_SET, in frame order
} g_test = {0, 0, 0, 0, -1, {}, 0, 0};
} g_test = {0, {}, 0, 0, 0, 0, -1, {}, 0, 0};
static void test_parse(void) {
g_test.parsed = 1;
if (const char *e = cfg_get("NX2NAVI_MENU_TEST")) {
long long at = atoll(e), per = 0;
long long per = 0;
if (const char *c = strchr(e, ':')) per = atoll(c + 1);
g_test.at = at > 0 ? (uint64_t)at : 0;
for (const char *s = e; *s && *s != ':' && g_test.n_at < TEST_OPENS;) {
char *end;
unsigned long long at = strtoull(s, &end, 10);
if (end == s) break;
if (at > 0 && (!g_test.n_at || at > g_test.at[g_test.n_at - 1])) g_test.at[g_test.n_at++] = at;
s = *end == ',' ? end + 1 : end;
}
g_test.per_page = per > 0 ? (uint64_t)per : TEST_PAGE_FRAMES;
}
const char *s = cfg_get("NX2NAVI_MENU_TEST_SET");
@@ -561,22 +595,30 @@ static void test_parse(void) {
while (*s && *s != ';') s++;
if (*s == ';') s++;
}
if (g_test.at || g_test.n_sets)
rt_log("menu: test hook: open at frame %llu, %llu frames per page, %d settings changes",
(unsigned long long)g_test.at, (unsigned long long)g_test.per_page, g_test.n_sets);
if (g_test.n_at || g_test.n_sets)
rt_log("menu: test hook: open at frame %llu (%d times), %llu frames per page, %d settings changes",
(unsigned long long)(g_test.n_at ? g_test.at[0] : 0), g_test.n_at, (unsigned long long)g_test.per_page,
g_test.n_sets);
}
static void test_apply_sets(uint64_t frame) {
for (; g_test.next_set < g_test.n_sets && g_test.sets[g_test.next_set].frame <= frame; g_test.next_set++) {
const TestSet &t = g_test.sets[g_test.next_set];
rt_log("menu: test hook: frame %llu: %s=%s", (unsigned long long)frame, t.key, t.value);
// a room's secret stays out of the log here too
const bool secret = !strcmp(t.key, "NX2NAVI_COOP_JOIN") || !strcmp(t.key, "NX2NAVI_COOP_ROOM");
rt_log("menu: test hook: frame %llu: %s=%s", (unsigned long long)frame, t.key, secret ? "(set, not shown)" : t.value);
if (!strcmp(t.key, "NX2NAVI_SAVE")) {
rt_log("menu: test hook: cfg_save %s", cfg_save() ? "wrote" : "failed");
} else if (!strcmp(t.key, "NX2NAVI_FOCUS")) {
char row[80];
snprintf(row, sizeof row, "%s%s", strncmp(t.value, "NX2NAVI_", strlen("NX2NAVI_")) ? "NX2NAVI_" : "", t.value);
opt_focus_key(row);
} else {
} else if (!strncmp(t.key, "NX2NAVI_COOP_", 13) && !cfg_from_env("NX2NAVI_MENU_TEST_SET")) {
// From nx2navi.ini the hook sets no co-op key and drives no part of the Co-op tab: a settings file
// someone hands a player could otherwise turn co-op on, join a room and accept its offer for them
// (COOP_JOIN, COOP_ACCEPT_OFFER). A test sets it in the environment.
rt_log("menu: test hook: %s is taken from the environment only, not from nx2navi.ini", t.key);
} else if (!coop_test_set(t.key, t.value)) {
cfg_set(t.key, t.value);
}
}
@@ -1402,7 +1444,16 @@ static void tab_controls(float scale) {
binds_after_page();
}
static const UiTextId kTabNames[TAB_COUNT] = {UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_SOUND, UI_MENU_TAB_CONTROLS};
// The Co-op tab (ui/coop_page.cpp): the rows, then the window a row opens.
static void tab_coop(float scale) {
opt_begin("##coop", scale);
coop_rows(scale);
opt_end();
coop_after_page(scale);
}
static const UiTextId kTabNames[TAB_COUNT] = {UI_MENU_TAB_SCREEN, UI_MENU_TAB_GAME, UI_MENU_TAB_SOUND,
UI_MENU_TAB_CONTROLS, UI_MENU_TAB_COOP};
static int g_tab; // the tab shown
static int g_tab_request = -1; // select this tab at the next layout; -1 none
static int g_focus_page = -1; // this tab's first row takes the cursor once it is shown; -1 none
@@ -1488,7 +1539,9 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) {
ImGui::TextDisabled("%s", version);
}
int step = g_modal || binds_modal() ? 0
// a text field takes Q and E as letters, and a window of a page keeps the tab keys
const bool keeps_keys = g_modal || binds_modal() || coop_modal() || ImGui::GetIO().WantTextInput;
int step = keeps_keys ? 0
: ImGui::IsKeyPressed(ImGuiKey_GamepadR1, false) || ImGui::IsKeyPressed(ImGuiKey_E, false) ? 1
: ImGui::IsKeyPressed(ImGuiKey_GamepadL1, false) || ImGui::IsKeyPressed(ImGuiKey_Q, false) ? -1
: 0;
@@ -1520,6 +1573,7 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) {
case TAB_GAME: tab_game(scale); break;
case TAB_SOUND: tab_sound(scale); break;
case TAB_CONTROLS: tab_controls(scale); break;
case TAB_COOP: tab_coop(scale); break;
}
ImGui::EndTabItem();
}
@@ -1579,7 +1633,7 @@ static void layout_menu(uint32_t w, uint32_t h, float scale) {
if (skin_widget_button(quit, ImVec2(bw, 0), scale)) g_confirm_quit = true, ImGui::OpenPopup("##confirm");
restart_confirm(scale);
menu_sounds_frame();
bool was_modal = g_modal || binds_modal();
bool was_modal = g_modal || binds_modal() || coop_modal() || ImGui::GetIO().WantTextInput;
if (!was_modal && (ImGui::IsKeyPressed(ImGuiKey_Escape, false) || ImGui::IsKeyPressed(ImGuiKey_GamepadFaceRight, false)))
set_open(0);
ImGui::End();
@@ -1735,7 +1789,7 @@ static void perf_draw(const MenuTarget *t, float scale) {
dl->AddText(ImVec2(p.x + padx, p.y + pady), SKIN_TEXT, g_perf_text);
}
extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot) {
static int overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot) {
*shot = NULL;
if (t->phys && t->phys != g_gpu_vendor_of) { // the GPU's vendor for the rows that depend on it (dlss_offered)
VkPhysicalDeviceProperties props;
@@ -1750,6 +1804,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha
if (g_perf_save.exchange(0)) cfg_save();
perf_tick(plat_now_ns(), t);
amiibo_tick();
const bool toast = coop_toast_tick(); // a co-op player came or went: a toast over the game, the menu closed
if (g_booting.load(std::memory_order_relaxed)) g_booting.store(0, std::memory_order_release); // the game runs
bool open = g_open.load(std::memory_order_relaxed);
// the frame the menu is first laid out after opening: also when its g_reset went to an earlier frame of the overlay
@@ -1759,13 +1814,13 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha
was_open = open;
if (!open) {
if (g_save_pending) g_save_pending = opt_save_tick(1); // closed: the last change goes to nx2navi.ini now
if (g_test.at && !g_test.opened && frame >= g_test.at) {
if (g_test.next_at < g_test.n_at && !g_test.opened && frame >= g_test.at[g_test.next_at]) {
g_test.opened = frame;
g_test.page = 0;
g_tab_request = 0;
set_open(1);
open = true;
} else if (!g_perf_mode) {
} else if (!g_perf_mode && !toast) {
return 0;
}
}
@@ -1799,6 +1854,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha
io.ClearInputKeys();
io.ClearInputMouse();
binds_cancel(); // a cell waiting for a press when the menu closed: no press binds on the next opening
coop_cancel(); // the Co-op tab's window closes and its secret hides
g_sound_fresh = true;
g_last_ns = 0;
g_focus_page = g_tab_request >= 0 ? g_tab_request : g_tab; // opening: the first row has the cursor
@@ -1823,6 +1879,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha
ImGui::NewFrame();
if (open) layout_menu(t->width, t->height, scale);
perf_draw(t, scale);
if (!open && toast) coop_toast_draw((float)t->width, (float)t->height, scale);
ImGui::Render();
if (open) g_save_pending = opt_save_tick(0);
g_cost_ns += plat_now_ns() - t0;
@@ -1833,6 +1890,7 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha
*shot = path;
if (++g_test.page == TAB_COUNT) {
g_test.page = -1;
g_test.opened = 0, g_test.next_at++; // the next NX2NAVI_MENU_TEST frame opens it again
set_open(0); // the next frame lays out nothing
} else {
g_tab_request = g_test.page;
@@ -1841,6 +1899,16 @@ extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const cha
return ImGui::GetDrawData()->TotalVtxCount > 0;
}
// A text field takes typing: the window delivers characters (and a handheld shows its keyboard) while one is active.
// Every overlay frame lets the window follow, also those overlay_frame leaves early because the menu is closed: when
// only the frames the menu laid out did, closing it with a field active (F1, Minus and Plus) left SDL's text input on,
// and on Windows the input method attached to the game window, until the menu opened again.
extern "C" int menu_overlay_frame(uint64_t frame, const MenuTarget *t, const char **shot) {
int r = overlay_frame(frame, t, shot);
menu_text_input_follow(g_ctx_ok && g_open.load(std::memory_order_relaxed) && ImGui::GetIO().WantTextInput);
return r;
}
// ---- the boot screen (boot.c's wait for the prewarm; gfx_present_boot presents it on a black frame)
static std::atomic<int> g_boot_phase;