From 9dc738c045fe34eb33814666e9678fb361547ef7 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Wed, 28 Oct 2026 14:49:41 +0300 Subject: [PATCH 1/4] Count untimed script lines in 60 Hz ticks of game time instead of presents, so the title menus get scripted presses at the same game time at any frame limit; in a recording the untimed lines stop when the world clock starts, and the recorder cuts a button or stick held at that moment and continues it as a timed event Co-Authored-By: Claude Opus 5.5 --- docs/controls.md | 4 ++-- runtime/frame_rate.c | 9 ++++++++ runtime/host.h | 1 + runtime/input_win.c | 52 +++++++++++++++++++++++++++++++------------- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/docs/controls.md b/docs/controls.md index 7745a09c..51896224 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -60,9 +60,9 @@ The stick direction bits (`StickLLeft` to `StickRDown`) are derived from the sti @ LSTICK|RSTICK [hold_ms] ``` -`` counts presented frames. `@` counts milliseconds of the world clock: game time stepped since the end of the first save load (the player leaves the title screen's origin), so a timed event lands at the same game time at any frame rate and load length. A later load, such as fast travel, still shifts what follows. Each event logs once (`input: script frame N: A`). `tools/input/load_save.txt` loads the first save from the title screen. +`` counts 60 Hz ticks of game time since the first frame (presented frames at 60 fps), so the title menus get the presses at the same game time at any frame limit. `@` counts milliseconds of the world clock: game time stepped since the end of the first save load (the player leaves the title screen's origin), so a timed event lands at the same game time at any frame rate and load length. A later load, such as fast travel, still shifts what follows. In a script with timed lines the untimed ones stop when the world clock starts, as the load ends at another tick in each run. Each event logs once (`input: script frame N tick T: A`). `tools/input/load_save.txt` loads the first save from the title screen. -`TOTK_INPUT_RECORD=` (`input_record` in `totk.ini`, or Input recording in the menu) writes the merged input in the same format, one line per button press and per stick segment: events before the world clock starts by frame, the rest timed. The file replays as a script, which makes it the repro to attach to a bug report. +`TOTK_INPUT_RECORD=` (`input_record` in `totk.ini`, or Input recording in the menu) writes the merged input in the same format, one line per button press and per stick segment: events before the world clock starts by tick, the rest timed; a button or stick held when the world clock starts is cut there and goes on as a timed event. The file replays as a script, which makes it the repro to attach to a bug report. ## RenderDoc capture (blocked) diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 5af49495..c5c2818b 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -49,12 +49,14 @@ #define STEP_FIRST_S (1.0 / 60.0) static _Atomic uint64_t *step_word; // patch page +0: ratio (low float), seconds (high float) +static double step_now = STEP_FIRST_S; // the step the patch page holds: what the game steps each frame static _Atomic uint32_t *step_interval; // patch page +8: seconds * 60 (patches-v143.txt realtime) static uint64_t last_ns; static double samples[STEP_SAMPLES]; static unsigned sample_count, sample_next; static void step_store(float seconds) { + step_now = seconds; float ratio = seconds * 30.0f, interval = seconds * 60.0f; uint32_t lo, hi, iv; memcpy(&lo, &ratio, 4); @@ -217,9 +219,11 @@ static void speed_report(uint64_t now, double step, int interval) { } static _Atomic int64_t world_us = -1; +static _Atomic uint64_t game_us; // game time stepped since the first present (frame_step_ticks) static uint64_t presents; int64_t frame_step_world_us(void) { return atomic_load_explicit(&world_us, memory_order_relaxed); } +uint64_t frame_step_ticks(void) { return atomic_load_explicit(&game_us, memory_order_relaxed) * 60 / 1000000; } static void world_clock(double dt, double step) { presents++; @@ -253,6 +257,11 @@ void frame_step_present(uint64_t display_ns, int interval) { } } last_ns = display_ns; + // The game clock of untimed script lines: what the game stepped this frame, the patch page's step with the + // real-time step (kept through a reset), whole vsyncs without it. + double game = step_word ? step_now : (interval > 0 ? interval : 1) / 60.0; + atomic_store_explicit(&game_us, atomic_load_explicit(&game_us, memory_order_relaxed) + (uint64_t)(game * 1e6 + 0.5), + memory_order_relaxed); world_clock(dt, step); if (rt_trace & RT_TRACE_PERF) speed_report(display_ns, step, interval); } diff --git a/runtime/host.h b/runtime/host.h index 137e6179..72de7c81 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -249,6 +249,7 @@ uint64_t frame_step_cap_ns(void); // with the real-time double frame_step_limit_hz(void); // the frame rate the limit gives (overlay); 0 unknown int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before // (TOTK_FPS_LIMIT, live) +uint64_t frame_step_ticks(void); // game time since the first present in 60 Hz ticks (input scripts) void frame_step_present(uint64_t display_ns, int interval); // nvnQueuePresentTexture: the frame's display time and // present interval (60 Hz ticks) // ---- boot (boot.c, main.c) diff --git a/runtime/input_win.c b/runtime/input_win.c index c2a2553f..390fb31f 100644 --- a/runtime/input_win.c +++ b/runtime/input_win.c @@ -41,10 +41,13 @@ static const SDL_GamepadButton pad_sdl[PAD_COUNT] = { }; // TOTK_INPUT_SCRIPT=: lines "